<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <input id="input" type="text" />
  <button id="button">发送</button>
  <script>
    window.onload = function () {
      var ws = new WebSocket("ws://localhost:2333");
      var input = document.getElementById("input");
      var button = document.getElementById("button");
      ws.onopen = function () {
        button.onclick = function () {
          console.log("input.value", input.value)
          ws && ws.send(input.value);
        }
      }

      ws.onmessage = function (event) {
        var message = event.data;

        var p = document.createElement("p");
        p.innerHTML = message;
        document.body.appendChild(p)
      }
    }

  </script>
</body>

</html>